from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
from address.models import Area


class User(AbstractUser):
    """用户模型"""

    mobile = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    email_active = models.BooleanField(default=False, verbose_name='邮箱已激活')
    default_address = models.ForeignKey('Address', models.SET_NULL, 'is_default',
                                        verbose_name='默认地址', null=True, blank=True)

    class Meta:
        db_table = 'tb_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class Address(models.Model):
    """
    收货地址
    """
    title = models.CharField(verbose_name='标题', max_length=50)
    user = models.ForeignKey(User, models.CASCADE, 'address', verbose_name='用户')
    consignee = models.CharField(verbose_name='收件人', max_length=50)
    mobile = models.CharField(verbose_name='手机号', max_length=11)
    province = models.ForeignKey(Area, models.PROTECT, 'province_address', verbose_name='省')
    city = models.ForeignKey(Area, models.PROTECT, 'city_address2', verbose_name='市')
    area = models.ForeignKey(Area, models.PROTECT, 'area_address3', verbose_name='区')
    place = models.CharField(verbose_name='地址', max_length=100)
    tel = models.CharField(verbose_name='固话', max_length=20, null=True, blank=True)
    email = models.CharField(verbose_name='邮箱', max_length=30, null=True, blank=True)
    is_delete = models.BooleanField('逻辑删除', default=False)

    class Meta:
        db_table = 'tb_address'
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title
